#include "SQLPartitionByHash.h"
#include "../../sql/ast/SQLPartition.h"
#include "../../sql/visitor/SQLASTVisitor.h"
#include "SQLSubPartitionBy.h"
#include "../../utils/listTran.h"
#include "../../utils/log.h"

BOOL_ptr SQLPartitionByHash::isKey()
{
  return key;
}

void SQLPartitionByHash::setKey(BOOL_ptr key)
{
  this->key = key;
}

BOOL_ptr SQLPartitionByHash::isUnique()
{
  return unique;
}

void SQLPartitionByHash::setUnique(BOOL_ptr unique)
{
  this->unique = unique;
}

// @Override
void SQLPartitionByHash::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLPartitionByHash)))
  {
    SQLObject_list_ptr  tmp1 = ListTran::tran_class<SQLExpr, SQLObject>(columns);
    acceptChild(visitor, tmp1);
    acceptChild(visitor, partitionsCount);
    SQLObject_list_ptr  tmp2 = ListTran::tran_class<SQLPartition, SQLObject>(getPartitions());
    acceptChild(visitor, tmp2);
    acceptChild(visitor, SQLSubPartitionBy_ptr(subPartitionBy));
  }
  visitor->endVisit(SharedObject(SQLPartitionByHash));
}

SQLObject_ptr SQLPartitionByHash::clone()
{
  SQLObject_ptr clone_tmp = SQLPartitionByHash_ptr(new SQLPartitionByHash());
  SQLPartitionByHash_ptr x = std::dynamic_pointer_cast<SQLPartitionByHash>(clone_tmp);
  // SQLPartitionByHash *x = new SQLPartitionByHash();

  cloneTo(x);

  x->key = key;
  x->unique = unique;

  for (SQLExpr_ptr column : *columns)
  {
    SQLExpr_ptr c2 = std::dynamic_pointer_cast<SQLExpr>(column->clone());
    c2->setParent(x);
    x->columns->push_back(c2);
  }

  return x;
}

void SQLPartitionByHash::cloneTo(SQLPartitionByHash_ptr x)
{
  SQLPartitionBy::cloneTo(x);
}
